package stz.flash.main;

import net.rim.device.api.animation.AnimatedScalar;
import net.rim.device.api.animation.Animation;
import net.rim.device.api.animation.Animator;
import net.rim.device.api.animation.AnimatorListener;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.UiApplication;
import stz.flash.main.state.AnimateModeState;
import stz.flash.main.state.KeyOperator;
import util.DjDoneListener;


public class AniEffect {

	public static int DEFAULT_EASING = Animation.EASINGCURVE_QUARTIC_OUT;
	public static int DEFAULT_DURATION = 1200;
	
	private static Animator animator = new Animator(30);
	private static AnimatedScalar scalar = new AnimatedScalar(0);
	private static Animation ani;
	private static DjDoneListener doneListener;
	static{
		animator.setAnimatorListener(new AniListener()); 
		
	}
	
	static public void nextDirection(int direction) {
		if (direction == 1) {
			ani = animator.addAnimationTo(scalar,
					AnimatedScalar.ANIMATION_PROPERTY_SCALAR,
					0,
					DEFAULT_EASING,
					DEFAULT_DURATION);
		} else {
			ani = animator.addAnimationTo(scalar,
					AnimatedScalar.ANIMATION_PROPERTY_SCALAR,
					-Display.getWidth(), 
					DEFAULT_EASING,
					DEFAULT_DURATION);
		}
	}
	
	static public void begin(long l) {
		ani.begin(l);
	}

	static public int getInt() {
		return scalar.getInt();
	}

	public static void setInt(int i) {
		scalar.setInt(i);
	}
	
	static class AniListener implements AnimatorListener {
		
		public void animatorUpdate() {
			UiApplication.getUiApplication().relayout();
		}
		
		public void animatorProcessing(boolean aniStart) {
			if (aniStart) {
				KeyOperator.setState(AnimateModeState.getInstance());
			} else {
				KeyOperator.setState(KeyOperator.getNextState());
				if (doneListener != null)
					doneListener.done();
			}
		}
	}
}
